﻿using System;
using System.Data;
using System.Data.SqlClient;
namespace PPI.Data
{
    public class DVendor_Visit_XMissionStructures_Image
    {
        #region Query

        public static DataSet GetServerImages(int offset, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_Visit_XMissionStructures_Images_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure; 
                da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@offset", ParameterDirection.Input, false, offset));
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_Visit_XMissionStructures_Images_SelectAll", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static DataSet GetByStructureID(int structureID, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_Visit_XMissionStructures_Images_SelectAllByStructureID", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructureFK", ParameterDirection.Input, false, structureID));
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_Visit_XMissionStructures_Images_SelectAllByStructureID", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static int GetFreePosition(int imageID, int structureID, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_Visit_XMissionStructures_Images_GetFreePosition", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@OriginalImageID", ParameterDirection.Input, false, imageID));
                da.SelectCommand.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructureFK", ParameterDirection.Input, false, structureID));
                if (da.SelectCommand.ExecuteScalar() != null)
                    return (int)da.SelectCommand.ExecuteScalar();
                else
                    return 0;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_Visit_XMissionStructures_Images_GetFreePosition", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        #endregion Query

        #region Crud

        public static int Insert(int vendor_Visit_XMissionStructureFK, bool solutionAvaible, string fileName, double latitude, double altitude, double longitude, DateTime satelliteTime, int satelliteCount, double direction, int vendor_Visit_XMissionStructures_PoleID, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_Images_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructureID", ParameterDirection.Input, false, vendor_Visit_XMissionStructureFK));
                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@FileName", 200, ParameterDirection.Input, false, fileName));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, solutionAvaible));
                cmd.Parameters.Add(DBHelper.CreateImageParameter("@Bytes", 0, ParameterDirection.Input, true, DBNull.Value));

                #region Vendor_Visit_XMissionStructures_PoleID
                if (vendor_Visit_XMissionStructures_PoleID == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructures_PoleID", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructures_PoleID", ParameterDirection.Input, false, vendor_Visit_XMissionStructures_PoleID));

                }
                #endregion

                #region Direction
                if (direction == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Direction", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Direction", ParameterDirection.Input, false, direction));
                }
                #endregion

                #region Latitude
                if (latitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, latitude));
                }
                #endregion

                #region Longitude
                if (longitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, longitude));

                }
                #endregion

                #region Altitude
                if (altitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, altitude));

                }
                #endregion

                #region SatelliteCount

                if (satelliteCount == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, satelliteCount));
                }



                #endregion

                #region SatelliteTime
                if (satelliteTime == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, satelliteTime));
                }

                #endregion


                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructures_ImageId", ParameterDirection.ReturnValue, false, 0));
                cmd.ExecuteNonQuery();
                tr.Commit();
                //UpdateFileName((Int32)cmd.Parameters["@Vendor_Visit_XMissionStructures_ImageId"].Value, vendor_Visit_XMissionStructureFK, fileName, connectionKey);
                return (Int32)cmd.Parameters["@Vendor_Visit_XMissionStructures_ImageId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }

        }

        public static void UpdateFileName(int imageID, int vendor_Visit_XMissionStructureFK, string filename, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_Images_Update_FileName", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructures_ImageId", ParameterDirection.Input, false, imageID));
                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@FileName", 200, ParameterDirection.Input, false, filename));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void InsertExistingImage(bool solutionAvaible, string fileName, double latitude, double altitude, double longitude, DateTime satelliteTime, int satelliteCount, double direction, int vendor_Visit_XMissionStructures_PoleID, int originalImageId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_Images_InsertExistingImage", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@OriginalImageID", ParameterDirection.Input, false, originalImageId));
                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Vendor_Visit_XMissionStructures_PoleID", 200, ParameterDirection.Input, false, vendor_Visit_XMissionStructures_PoleID));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, solutionAvaible));

                #region Direction
                if (direction == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Direction", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Direction", ParameterDirection.Input, false, direction));
                }
                #endregion

                #region Latitude
                if (latitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, latitude));
                }
                #endregion

                #region Longitude
                if (longitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, longitude));

                }
                #endregion

                #region Altitude
                if (altitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, altitude));

                }
                #endregion

                #region SatelliteCount

                if (satelliteCount == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, satelliteCount));
                }



                #endregion

                #region SatelliteTime
                if (satelliteTime == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, satelliteTime));
                }

                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void UpdatePoleReference(bool solutionAvaible, double latitude, double altitude, double longitude, DateTime satelliteTime, int satelliteCount, double direction, int vendor_Visit_XMissionStructures_PoleID, int originalImageId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_Images_UpdatePoleReference", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Visit_XMissionStructures_ImageId", ParameterDirection.Input, false, originalImageId));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@SolutionAvailable", ParameterDirection.Input, false, solutionAvaible));

                #region PoleID
                if (vendor_Visit_XMissionStructures_PoleID != Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Vendor_Visit_XMissionStructures_PoleID", 200, ParameterDirection.Input, false, vendor_Visit_XMissionStructures_PoleID));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Vendor_Visit_XMissionStructures_PoleID", 200, ParameterDirection.Input, true, DBNull.Value));
                #endregion

                #region Latitude
                if (latitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, latitude));
                }
                #endregion

                #region Longitude
                if (longitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, longitude));

                }
                #endregion

                #region Altitude
                if (altitude == double.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, true, DBNull.Value));

                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Altitude", ParameterDirection.Input, false, altitude));

                }
                #endregion

                #region SatelliteCount

                if (satelliteCount == Int32.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SatelliteCount", ParameterDirection.Input, false, satelliteCount));
                }



                #endregion

                #region SatelliteTime
                if (satelliteTime == DateTime.MinValue)
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@SatelliteTime", ParameterDirection.Input, true, satelliteTime));
                }

                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Delete(int imageID, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Visit_XMissionStructures_Images_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@ImageID", ParameterDirection.Input, false, imageID));
                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        #endregion
    }
}
